package com.xxyf.controller;

import com.xxyf.domain.PageParams;
import com.xxyf.entity.Message;
import com.xxyf.entity.Reply;
import com.xxyf.service.MessageService;
import com.xxyf.tools.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 小小怡飞
 * @Date 2023/3/2 13:14
 * @Version JDK 8
 */
@RestController
@Api(tags = "留言管理")
@RequestMapping("/msg")
@Slf4j
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MessageApi {
    @Autowired
    private MessageService messageService;

    @GetMapping("/query/{userid}")
    @ApiOperation("查询留言")
    public Rest query(PageParams pageParams,@PathVariable("userid") Long userid){
       return messageService.query(pageParams,userid);
    }
    @ApiOperation("创建评论")
    @PreAuthorize("hasAuthority('sys:user')")
    @PostMapping("/crate")
    public Rest crate(@ApiParam("留言版") @RequestBody Message message){
        return  messageService.crate(message);
    }
    @PreAuthorize("hasAuthority('sys:user')")
    @ApiOperation("回复评论")
    @PostMapping("/insert")
    public Rest insert(@ApiParam("回复版")@RequestBody Reply reply){
        return messageService.insert(reply);
    }
    @ApiOperation("删除评论")
    @PreAuthorize("hasAuthority('sys:user')")
    @DeleteMapping("/delete/{type}/{id}/{userid}")
    public Rest delete(@ApiParam("根据类型选择留言表还是回复表")@PathVariable("type")String type,
                       @PathVariable("id") Long id,@PathVariable("userid") Long userid){
        return messageService.delete(type,id,userid);
    }
    @ApiOperation("点赞")
    @PreAuthorize("hasAuthority('sys:user')")
    @GetMapping("/like/{type}/{pid}/{userid}")
    public Rest like(@PathVariable("pid")Long pid,@PathVariable("userid") Long userid,
    @PathVariable("type") String type
    ){
        return messageService.like(type,pid,userid);
    }
    @ApiOperation("取消点赞")
    @PreAuthorize("hasAuthority('sys:user')")
    @DeleteMapping("/notLike/{type}/{pid}/{userid}")
    public Rest notLike(@PathVariable("pid")Long pid,@PathVariable("userid") Long userid,
                        @PathVariable("type") String type){
        return messageService.notLike(type,pid,userid);
    }
}
